home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_197 / stevie / main.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  10KB  |  368 lines

  1. /*
  2.  * STEVIE - Simply Try this Editor for VI Enthusiasts
  3.  *
  4.  * Code Contributions By : Tim Thompson           twitch!tjt
  5.  *                         Tony Andrews           onecom!wldrdg!tony 
  6.  *                         G. R. (Fred) Walter    watmath!watcgl!grwalter 
  7.  */
  8.  
  9. #include "stevie.h"
  10.  
  11. int             Rows;        /* Number of Rows and Columns */
  12. int             Columns;    /* in the current window. */
  13.  
  14. char           *Realscreen = NULL;    /* What's currently on the screen, a
  15.                      * single array of size Rows*Columns. */
  16. char           *Nextscreen = NULL;    /* What's to be put on the screen. */
  17. int             NumLineSizes = 0;    /* # of active LineSizes */
  18. LINE          **LinePointers = NULL;    /* Pointer to the line for LineSizes */
  19. char           *LineSizes = NULL;    /* Size of a line (pline output) */
  20.  
  21. char           *Filename = NULL;/* Current file name */
  22.  
  23. LPtr           *Filemem;    /* The contents of the file, as a single
  24.                  * array. */
  25. LPtr           *Filetop;    /* Line 'above' the start of the file */
  26.  
  27. LPtr           *Fileend;    /* Pointer to the end of the file in Filemem.
  28.                  * (It points to the byte AFTER the last
  29.                  * byte.) */
  30.  
  31. LPtr           *Topchar;    /* Pointer to the byte in Filemem which is in
  32.                  * the upper left corner of the screen. */
  33.  
  34. LPtr           *Botchar;    /* Pointer to the byte in Filemem which is
  35.                  * just off the bottom of the screen. */
  36.  
  37. LPtr           *Curschar;    /* Pointer to byte in Filemem at which the
  38.                  * cursor is currently placed. */
  39.  
  40. int             Curscol;    /* Current position of cursor (column) */
  41. int             Cursrow;    /* Current position of cursor (row) */
  42.  
  43. int             Cursvcol;    /* Current virtual column, the column number
  44.                  * of the file's actual line, as opposed to
  45.                  * the column number we're at on the screen.
  46.                  * This makes a difference on lines that span
  47.                  * more than one screen line. */
  48.  
  49. int             Curswant = 0;    /* The column we'd like to be at. This is
  50.                  * used try to stay in the same column
  51.                  * through up/down cursor motions. */
  52.  
  53. bool_t          set_want_col;    /* If set, then update Curswant the next time
  54.                  * through cursupdate() to the current
  55.                  * virtual column. */
  56.  
  57. int             State = NORMAL;    /* This is the current state of the command
  58.                  * interpreter. */
  59.  
  60. int             Prenum = 0;    /* The (optional) number before a command. */
  61.  
  62. LPtr           *Insstart;    /* This is where the latest insert/append
  63.                  * mode started. */
  64.  
  65. bool_t          Changed = FALSE;/* Set to TRUE if something in the file has
  66.                  * been changed and not written out. */
  67.  
  68. char           *IObuff;        /* file reads are done, one line at a time,
  69.                  * into this buffer; as well as sprintf's */
  70.  
  71. char           *Insbuffptr = NULL;
  72. char           *Insbuff;    /* Each insertion gets stuffed into this
  73.                  * buffer. */
  74.  
  75. char           *Readbuffptr = NULL;
  76. char           *Readbuff;    /* Having this buffer allows STEVIE to easily
  77.                  * make itself do commands */
  78.  
  79. char           *Redobuffptr = NULL;
  80. char           *Redobuff;    /* Each command should stuff characters into
  81.                  * this buffer that will re-execute itself. */
  82.  
  83. bool_t          UndoInProgress = FALSE;    /* Set to TRUE if undo'ing */
  84. char           *Undobuffptr = NULL;
  85. char           *Undobuff;    /* Each command should stuff characters into
  86.                  * this buffer that will undo its effects. */
  87.  
  88. char           *UndoUndobuffptr = NULL;
  89. char           *UndoUndobuff;    /* Each command should stuff characters into
  90.                  * this buffer that will undo its undo. */
  91.  
  92. char           *Yankbuffptr = NULL;
  93. char           *Yankbuff;    /* Yank buffer */
  94.  
  95. char            last_command = NUL;    /* last command */
  96. char            last_command_char = NUL;    /* character needed to undo
  97.                          * last command */
  98.  
  99. bool_t          RedrawingDisabled = FALSE;    /* Set to TRUE if undo'ing or
  100.                          * put'ing */
  101.  
  102. bool_t          MustRedrawLine = FALSE;    /* Set to TRUE if we must redraw the
  103.                      * current line */
  104. bool_t          MustRedrawScreen = TRUE;    /* Set to TRUE if we must
  105.                          * redraw the screen */
  106.  
  107. char          **files;        /* list of input files */
  108. int             numfiles;    /* number of input files */
  109. int             curfile;    /* number of the current file */
  110.  
  111. static void
  112. usage()
  113. {
  114.     fprintf(stderr, "usage: stevie [file ...]\n");
  115.     fprintf(stderr, "       stevie -t tag\n");
  116.     fprintf(stderr, "       stevie +[num] file\n");
  117.     fprintf(stderr, "       stevie +/pat  file\n");
  118.     exit(1);
  119. }
  120.  
  121. #ifdef AMIGA
  122. void
  123. #else
  124. int
  125. #endif
  126. main(argc, argv)
  127.     int             argc;
  128.     char          **argv;
  129. {
  130.     char           *initstr, *getenv();    /* init string from the environment */
  131.     char           *tag = NULL;    /* tag from command line */
  132.     char           *pat = NULL;    /* pattern from command line */
  133.     int             line = -1;    /* line number from command line */
  134.  
  135.     int             atoi();
  136.  
  137. #ifdef AMIGA
  138. /*
  139.  * This won't be needed if you have a version of Lattice 4.01 without broken
  140.  * break signal handling.
  141.  */
  142.     (void) signal(SIGINT, SIG_IGN);
  143. #endif
  144.  
  145.     /*
  146.      * Process the command line arguments. 
  147.      */
  148.     if (argc > 1) {
  149.     switch (argv[1][0]) {
  150.  
  151.       case '-':        /* -t tag */
  152.         if (argv[1][1] != 't')
  153.         usage();
  154.  
  155.         if (argv[2] == NULL)
  156.         usage();
  157.  
  158.         Filename = NULL;
  159.         tag = argv[2];
  160.         numfiles = 1;
  161.         break;
  162.  
  163.       case '+':        /* +n or +/pat */
  164.         if (argv[1][1] == '/') {
  165.         if (argv[2] == NULL)
  166.             usage();
  167.         Filename = strsave(argv[2]);
  168.         pat = &(argv[1][1]);
  169.         numfiles = 1;
  170.  
  171.         } else if (isdigit(argv[1][1]) || argv[1][1] == NUL) {
  172.         if (argv[2] == NULL)
  173.             usage();
  174.         Filename = strsave(argv[2]);
  175.         numfiles = 1;
  176.  
  177.         line = (isdigit(argv[1][1])) ?
  178.             atoi(&(argv[1][1])) : 0;
  179.         } else
  180.         usage();
  181.  
  182.         break;
  183.  
  184.       default:        /* must be a file name */
  185.         Filename = strsave(argv[1]);
  186.         files = &(argv[1]);
  187.         numfiles = argc - 1;
  188.         break;
  189.     }
  190.     } else {
  191.     Filename = NULL;
  192.     numfiles = 1;
  193.     }
  194.     curfile = 0;
  195.  
  196.     windinit();
  197.  
  198.     /*
  199.      * Allocate LPtr structures for all the various position pointers 
  200.      */
  201.     if ((Filemem = (LPtr *) alloc((unsigned) sizeof(LPtr))) == NULL) {
  202.     fprintf(stderr, "Can't allocate data structures\n");
  203.     windexit(0);
  204.     }
  205.     if ((Filetop = (LPtr *) alloc((unsigned) sizeof(LPtr))) == NULL) {
  206.     fprintf(stderr, "Can't allocate data structures\n");
  207.     windexit(0);
  208.     }
  209.     if ((Fileend = (LPtr *) alloc((unsigned) sizeof(LPtr))) == NULL) {
  210.     fprintf(stderr, "Can't allocate data structures\n");
  211.     windexit(0);
  212.     }
  213.     if ((Topchar = (LPtr *) alloc((unsigned) sizeof(LPtr))) == NULL) {
  214.     fprintf(stderr, "Can't allocate data structures\n");
  215.     windexit(0);
  216.     }
  217.     if ((Botchar = (LPtr *) alloc((unsigned) sizeof(LPtr))) == NULL) {
  218.     fprintf(stderr, "Can't allocate data structures\n");
  219.     windexit(0);
  220.     }
  221.     if ((Curschar = (LPtr *) alloc((unsigned) sizeof(LPtr))) == NULL) {
  222.     fprintf(stderr, "Can't allocate data structures\n");
  223.     windexit(0);
  224.     }
  225.     if ((Insstart = (LPtr *) alloc((unsigned) sizeof(LPtr))) == NULL) {
  226.     fprintf(stderr, "Can't allocate data structures\n");
  227.     windexit(0);
  228.     }
  229.     /*
  230.      * Allocate space for the many buffers 
  231.      */
  232.     if ((IObuff = alloc(IOSIZE)) == NULL) {
  233.     fprintf(stderr, "Can't allocate data structures\n");
  234.     windexit(0);
  235.     }
  236.     if ((Insbuff = alloc(INSERT_SIZE)) == NULL) {
  237.     fprintf(stderr, "Can't allocate data structures\n");
  238.     windexit(0);
  239.     }
  240.     if ((Readbuff = alloc(READSIZE)) == NULL) {
  241.     fprintf(stderr, "Can't allocate data structures\n");
  242.     windexit(0);
  243.     }
  244.     if ((Redobuff = alloc(REDO_UNDO_SIZE)) == NULL) {
  245.     fprintf(stderr, "Can't allocate data structures\n");
  246.     windexit(0);
  247.     }
  248.     if ((Undobuff = alloc(REDO_UNDO_SIZE)) == NULL) {
  249.     fprintf(stderr, "Can't allocate data structures\n");
  250.     windexit(0);
  251.     }
  252.     if ((UndoUndobuff = alloc(REDO_UNDO_SIZE)) == NULL) {
  253.     fprintf(stderr, "Can't allocate data structures\n");
  254.     windexit(0);
  255.     }
  256.     if ((Yankbuff = alloc(YANKSIZE)) == NULL) {
  257.     fprintf(stderr, "Can't allocate data structures\n");
  258.     windexit(0);
  259.     }
  260.     screenalloc();
  261.     filealloc();        /* Initialize Filemem, Filetop & Fileend */
  262.  
  263.     screenclear();
  264.  
  265.     if ((initstr = getenv("EXINIT")) != NULL) {
  266.     char           *lp, buf[128];
  267.  
  268.     if ((lp = getenv("LINES")) != NULL) {
  269.         sprintf(buf, "%s lines=%s", initstr, lp);
  270.         readcmdline(':', buf);
  271.     } else
  272.         readcmdline(':', initstr);
  273.     }
  274.     if (Filename != NULL) {
  275.     if (readfile(Filename, Filemem, FALSE))
  276.         filemess("[New File]");
  277.     } else
  278.     msg("Empty Buffer");
  279.  
  280.     setpcmark();
  281.  
  282.     updateNextscreen(NOT_VALID);
  283.  
  284.     if (tag) {
  285.     stuffReadbuff(":ta ");
  286.     stuffReadbuff(tag);
  287.     stuffReadbuff("\n");
  288.  
  289.     } else if (pat) {
  290.     stuffReadbuff(pat);
  291.     stuffReadbuff("\n");
  292.  
  293.     } else if (line >= 0) {
  294.     if (line > 0)
  295.         stuffnumReadbuff(line);
  296.     stuffReadbuff("G");
  297.     }
  298.     edit();
  299.  
  300.     windexit(0);
  301. }
  302.  
  303. void
  304. stuffReadbuff(s)
  305.     char           *s;
  306. {
  307.     if (strlen(s) == 0)
  308.     return;
  309.  
  310.     if (Readbuffptr == NULL) {
  311.     if ((strlen(s) + 1) < READSIZE) {
  312.         strcpy(Readbuff, s);
  313.         Readbuffptr = Readbuff;
  314.         return;
  315.     }
  316.     } else if ((strlen(Readbuff) + (strlen(s) + 1)) < READSIZE) {
  317.     strcat(Readbuff, s);
  318.     return;
  319.     }
  320.     emsg("Couldn't stuffReadbuff() - clearing Readbuff\n");
  321.     *Readbuff = NUL;
  322.     Readbuffptr = NULL;
  323. }
  324.  
  325. void
  326. stuffnumReadbuff(n)
  327.     int             n;
  328. {
  329.     char            buf[32];
  330.  
  331.     sprintf(buf, "%d", n);
  332.     stuffReadbuff(buf);
  333. }
  334.  
  335. /* OPTRESULT */
  336. char
  337. vgetc()
  338. {
  339.     int             c;
  340.  
  341.     /*
  342.      * inchar() may map special keys by using stuffReadbuff(). If it does so,
  343.      * it returns -1 so we know to loop here to get a real char. 
  344.      */
  345.     do {
  346.     if (Readbuffptr != NULL) {
  347.         char            nextc = *Readbuffptr++;
  348.  
  349.         if (*Readbuffptr == NUL) {
  350.         *Readbuff = NUL;
  351.         Readbuffptr = NULL;
  352.         }
  353.         return (nextc);
  354.     }
  355.     c = inchar();
  356.     } while (c == -1);
  357.  
  358.     return (char) c;
  359. }
  360.  
  361. char
  362. vpeekc()
  363. {
  364.     if (Readbuffptr != NULL)
  365.     return (*Readbuffptr);
  366.     return (NUL);
  367. }
  368.